import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/Index.vue')
    },
    {
      path: '/',
      component: () => import('@/views/layout/Index.vue'),
      redirect: '/layout/article/classfication',
      children: [
        {
          path: '/layout/article/classfication',
          component: () => import('@/views/article/Classfication.vue')
        },
        {
          path: '/layout/article/manage',
          component: () => import('@/views/article/Manage.vue')
        },
        {
          path: '/layout/user/basic',
          component: () => import('@/views/user/Basic.vue')
        },
        {
          path: '/layout/user/changeHeadImg',
          component: () => import('@/views/user/ChangeHeadImg.vue')
        },
        {
          path: '/layout/user/resetPassword',
          component: () => import('@/views/user/ResetPassword.vue')
        }
      ]
    }
  ],
})

// 登录访问拦截
router.beforeEach((to) => {
  const user = useUserStore()
  if (!user.token && to.path !== '/login') return '/login'
})
export default router
